package cunostinte;

import java.util.ArrayList;
import java.util.List;


public class ScenarioAnalzer {
	static List<Record> oameni=new ArrayList<Record>();

	public static Record parseLine(String line){
		String persoana="", locatia="", actiune="", ora1="",ora2="",locatia_p="";
		String token = " ";
		String[] tmp;

		Record current = null;
		if(line.startsWith("Creaza")){
			String pers = line.substring(7);
			Record om = new Record(pers,"","","","","","","","","");
			oameni.add(om);
			return om;
		}
		else{
			tmp = line.split(token);
			for (int i=0; i<oameni.size();i++){
				current = oameni.get(i);
				persoana = current.getPersoana();
				if(persoana.equals(tmp[0])){
					persoana = tmp[0];
					for (int j=1; j<tmp.length; j++){
						if(tmp[j].equals("in")){
							j++;
							locatia=tmp[j];
							
							continue;
						}
						if(tmp[j].equals("de")){
							j+=3;
							ora1 = tmp[j];
							continue;
						}
						if(tmp[j].equals("la")&&(tmp[j+1].equals("ora"))){
							j+=2;
							ora2 = tmp[j];
							continue;
						}
						actiune = actiune + tmp[j]+" ";
					}
					break;
				}
			}
		}
		actiune=actiune.trim();
		if(actiune.equals("pleaca")||actiune.equals("vine")) {
			current.updateLocation("");
		}
		if(actiune.equals("merge")){
			if(!current.getLocation().equals("")){
				locatia_p= current.getLocation();
			}
			current.updateLocation(locatia);
		}
		if(locatia.equals(""))
			locatia=current.getLocation();
		if(ora1.equals(""))
			ora1=ora2;
		String[] time1,time2;
		time1=ora1.split(":");
		time2=ora2.split(":");
		if((time1.length==3)&&(time2.length==3)){
			return new Record(persoana,actiune,locatia_p,locatia,time1[0],time1[1],time1[2],time2[0],time2[1],time2[2]);
		}else{
		return new Record(persoana,actiune,"",locatia,"","","","","","");
		}
	}
	
}

